package com.base.cn.platform.os.controller.web.user.perfect;

import com.base.cn.platform.os.common.WebBaseController;
import com.base.cn.platform.os.common.j2cache.CustomCacheChannel;
import com.base.cn.platform.os.common.rsa.WebRsaUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.service.manage.op.advert.OpAdvertService;
import com.base.cn.platform.os.service.manage.user.login.UserLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户完善资料Controller
 *
 * @author s.li
 * @create 2018-06-04-17:54
 */
@Controller
public class WebUserPerfectController extends WebBaseController{

    @Autowired
    private OpAdvertService opAdvertService;
    @Autowired
    private UserLoginService userLoginService;
    @Autowired
    private CustomCacheChannel customCacheChannel;

    /**
     * 完善专业页面
     * @param model
     * @param sessionId
     * @param autoThirty
     * @return
     */
    @RequestMapping("/web/user/perfectSubject")
    public String perfectSubject(Model model,
                                @RequestParam("key") String sessionId,
                                @RequestParam("autoThirty") String autoThirty){
        model.addAttribute("sessionId",sessionId);
        model.addAttribute("autoThirty",autoThirty);
        Map<String,Object> params = new HashMap<>();
        params.put("placeKey","LRBANNER");
        params.put("status", 1);
        List<Map<String, Object>> advertList = opAdvertService.queryAdvertList(params,false,false,false);//主页banner图片
        model.addAttribute("advertList",advertList);
        return resultPage("/user/perfect/perfect-subject");
    }

    /**
     * 完善手机号页面
     * @param model
     * @param sessionId 密钥KEY
     * @param autoThirty 是否是缓存30天
     * @return
     */
    @RequestMapping("/web/user/perfectMobile")
    public String perfectMobile(Model model,
                                @RequestParam("key") String sessionId,
                                @RequestParam("autoThirty") String autoThirty){
        model.addAttribute("sessionId",sessionId);
        model.addAttribute("autoThirty",autoThirty);
        Map<String,Object> params = new HashMap<>();
        params.put("placeKey","LRBANNER");
        params.put("status", 1);
        List<Map<String, Object>> advertList = opAdvertService.queryAdvertList(params,false,false,false);//主页banner图片
        model.addAttribute("advertList",advertList);
        return resultPage("/user/perfect/perfect-mobile");
    }


    /**
     * 第三方登录方式的设置登录密码
     * @return 操作页面
     */
    @RequestMapping("/web/uc/administratorPwd")
    public String administratorPwd(Model model) throws Exception{
        Map<String,Object> params = new HashMap<>();
        params.put("placeKey","LRBANNER");
        params.put("status", 1);
        List<Map<String, Object>> advertList = opAdvertService.queryAdvertList(params,false,false,false);//主页banner图片
        model.addAttribute("advertList",advertList);
        Map<String,String> keyMap = WebRsaUtils.getRSAData(customCacheChannel);
        model.addAttribute("keyMap",keyMap);
        return resultPage("/user/perfect/perfect-pwd");
    }

    /**
     * 设置登录密码
     * @param request HttpServletRequest
     * @param userPassword 用户密码
     * @param confirmPassword 用户砍确认密码
     * @return Map<String,Object>
     */
    @RequestMapping("/web/uc/setPerfectPwd")
    @ResponseBody
    public Map<String,Object> setPerfectPwd(HttpServletRequest request,
                                            @RequestParam("userPassword") String userPassword,
                                            @RequestParam("confirmPassword") String confirmPassword,
                                            @RequestParam("sign") String sign) throws Exception{
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        Map<String,Object> resultMap = userLoginService.setPerfectPwd(userId,userPassword,confirmPassword,sign);
        if(!ResultUtil.getSuccess(resultMap)){
            Map<String,String> keyMap = WebRsaUtils.getRSAData(customCacheChannel);
            resultMap.put("keyMap",keyMap);
        }
        return resultMap;
    }
}
